package List

import com.alibaba.fastjson.JSON
import org.apache.commons.lang3.StringUtils
import test.Book
import test.Student
import java.lang.StringBuilder
import java.math.BigDecimal
import java.util.*
import java.util.stream.Collectors
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

fun main() {
//    test04()
//test1()
//    test2()
//    test3()
//    test()
//    test5()
//    val s:String = ""
//    println(!s.isNullOrBlank())
//    test11()

//    test12()
//    test3()
//    test03_1()
//    test14()
//    test19()
//    test()
//    get()
//    getGUID()
//    getUUID()
//    getcccc()
//    test99()
//    test100()
//    test990()
    test101()
}
fun test990(){
    System.err.println(1 in listOf(11,2))
}


/**
 * 请勿修改这个方法
 * 需要添加学生的话，名字a1开始，年龄100开始
 */
fun getStudentList1(): List<Student> {
    val stuList: MutableList<Student> = ArrayList()

    val books = listOf<Book>(Book("Java", 12.0), Book("C", 13.0))
    stuList.add(Student("wade", 22, books))
    stuList.add(Student("www", 12, books))
    stuList.add(Student("ccc", 12, books))
    stuList.add(Student("ccc", 44, books))
    stuList.add(Student("vvv", 0, books))
    stuList.add(Student("kk", 11, null))


    val str = listOf<String>("", "")
    stuList.add(Student("ss", 11, books, str))

    val b: MutableList<Book> = ArrayList()
    stuList.add(Student("mm", 11, b))

    stuList.add(Student("", 0, books))
    return stuList
}

fun test14() {
    var list = getStudentList1().find { StringUtils.equalsIgnoreCase("ccc", it.name) }
    System.err.println(list)
}

fun test11() {
    val list = getStudentList1().map {
        Student(it.name, it.age - 1, it.books)
    }
    println(list)
}

fun test13() {
    val books = listOf<Book>(Book("Java", 12.0), Book("C++", 13.0))
    val list = getStudentList1().map {
        val str = listOf<String>("Java", "c")
        for (s in str) {
            books.filter {
                StringUtils.equals(s, it.bookName)
            }
        }
        Student(it.name, it.age)
    }
}

fun test12() {
    val list = listOf<Student>()
    val s = list.map {
        Student(it.name, it.age - 1, it.books)
    }
    println(s)
}

fun test19() {
    val s = getStudentList1()
    val t = s.map {
        if (it.name.equals("www")) {
            it.age = 100
        }
    }
    println(s)
    println(t)
}

/**
 * 转成map
 */
fun test1() {
    val ageMap = getStudentList1().map { it.age to it }.toMap()
    println(ageMap)
    //将map的key转成list
    val ageList = ageMap.keys.toList()
    println(ageList)
    //sortedByDescending降序
    val descAgeList = ageList.sortedByDescending { it }
    println(descAgeList)
}


fun test() {
    val stu = getStudentList1().sortedBy { it.age }

    val s = stu.stream().map { it.age }.collect(Collectors.toList())
    println(s)

}

fun test5() {
    val ages = listOf<Int>(0, 22)
    println(getStudentList1().filterNot {
        it.age in ages
    }.sortedBy { it.age })
}

private fun get() {
    val list = listOf<Int>(1,2,3,5,6,8,10)
    val n = 8
    val map = HashMap<Int, Int>()
    System.err.println("2")
    val listNew = mutableListOf<Int>()
    while (map.size < n) {
        System.err.println("11")
        val random = (Math.random() * list.size).toInt()
        if (!map.containsKey(random)) {
            map.put(random, 0);
            listNew.add(list.get(random))
        }
    }
    System.err.println(listNew)

}

/**
 * 16位大写或数字的编码
 */
fun getGUID(){
    val uid = StringBuilder();
    //产生16位的强随机数
    //产生16位的强随机数
    val rd = Random()
    for (i in 0..15) {
        //产生0-2的3位随机数
        when (rd.nextInt(3)) {
            0 ->           //0-9的随机数
                uid.append(rd.nextInt(10))
            1 ->           //ASCII在65-90之间为大写,获取大写随机
                uid.append((rd.nextInt(25) + 65).toChar())
        }
    }
    System.err.println(uid.toString())
}
fun getUUID(){
    for (i in 1..15){

        println(UUID.randomUUID().toString().replace("-", ""))
    }
}
fun getcccc(){
    println(StringUtils.containsAny("湖南","m南省", ))
}

fun test2() {
    //filter,按条件过滤，名字包含w的
    val list1 = getStudentList1().filter { it.name.contains("w") }
    println(list1)
    //拿到年龄最小的记录，maxBy,如果有多条最小，只返回一条？
    val minAge = list1.minByOrNull { it.age }
    println(minAge)

    val list2 = list1.groupBy { it.age }
    //{22=[Student{name='wade', age=22, books=null}],
    // 12=[Student{name='www', age=12, books=null}, Student{name='ww', age=12, books=null}]}
    println(list2)

    val list3 = getStudentList1().filter { it.name.contains("w") }
        .groupBy { it.age }.map { it.value[0] }
    println(list3)

}

fun test3() {
    val stuList = getStudentList1()
    for (i in 1..10) {
        println(stuList[0]?.books[i])
    }
}

fun test03_1() {
    val num = 2;
    if (num !in 1..2) {
        System.err.println("ddd")
    } else {
        System.err.println("ccc")
    }
}

fun test04() {
    val str = listOf<String>(
        "1、按时完成家庭作业，不会的题目，主动向家人或别人请教",
        "2、按时作息，能有条理地安排自己一天的学习和生活",
        "3、自己的房间自己整理、清洁、美化",
        "4、能承担洗衣、洗碗、拖地、倒垃圾等一些力所能及的家务活",
        "5、不挑吃穿，购买物品时，能和父母商量，不乱花钱",
        "6、关心体贴父母，心存感恩，父母生病时，能尽力照顾",
        "7、尊重父母的意见和教导，经常把学习、生活、思想情况告诉父母",
        "8、与父母产生误会时，能耐心向父母说明情况，不发脾气",
        "9、勇于承认错误，知错就改",
        "10、阅读、观看健康有益的图书、报刊、音像、网上信息和广播电视节目，不长时间看电视、玩游戏"
    )
    val json = JSON.toJSONString(str)
    println(json)
}

fun test99(){
    val s = mutableListOf(2,3,4,5,6)
    val c = mutableListOf(1,2,3)
    println(c.removeAll(s))
val p = 1
    val C = 2
println(99.5<100.0)
    println(BigDecimal(p.toDouble() / C.toDouble()).setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()*100)
}

fun test100(){
    val s = listOf<Int>()
    println(s.filter { it==1 })
}

/**
 */
fun test101(){
    val stuList: MutableList<Student> = ArrayList()

    val books = listOf<Book>(Book("Java", 12.0), Book("C", 13.0))
    stuList.add(Student("wade", 22, books))
    stuList.add(Student("www1", 12, books))
    stuList.add(Student("ccc", 12, books))
    stuList.add(Student("ccc", 44, books))
    stuList.add(Student("vvv", 0, books))
    println(stuList)
    println(stuList.indexOf(stuList.find { StringUtils.equals("www1",it.name) }))
//    stuList.map {
//        if(StringUtils.equals("www1",it.name) )
//            it.age = it.age+22
//        it
//    }
//    println(stuList)
    println("集合不为空："+ stuList.sumBy { it.age })
    val stuList1: MutableList<Student> = ArrayList()
    println("集合为空："+ stuList1.sumBy { it.age })
    stuList.forEach {
        if(StringUtils.equals("www1",it.name) )
            it.age = it.age+22
    }
    println(stuList)
//    val s = null?.status
}


